package com.zhx.websocket.signalr.controller;

import com.microsoft.signalr.HubConnection;
import com.microsoft.signalr.HubConnectionState;
import com.zhx.websocket.signalr.comm.CommContruct;
import com.zhx.websocket.signalr.config.SignalrClientConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-07-25 10:25
 * @description：用途:
 * @modified By：zhanghaixuan
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class SignalController {

    @Autowired
    private HubConnection hubConnection;

    @Autowired
    private SignalrClientConfig signalrClientConfig;

    @PostConstruct
    public void init(){
        log.info("开始连接");

        hubConnection.send(CommContruct.login_url,  CommContruct.token);

    }

    @GetMapping("/login")
    public String login(){
        hubConnection.send(CommContruct.login_url,  CommContruct.token);

        return "login";
    }


    @GetMapping("/together")
    public String together(){
        hubConnection.send(CommContruct.login_url,  CommContruct.token);
        hubConnection.send(CommContruct.test_url);
        return "together";
    }

    @GetMapping("/reconnect")
    public String reconnect(){
        HubConnection hubConnection1 = signalrClientConfig.hubConnection();
        this.hubConnection.send(CommContruct.login_url,  CommContruct.token);
        this.hubConnection.send(CommContruct.test_url);
        return "reconnect";
    }

    @GetMapping("/states")
    public String states(){
        HubConnectionState connectionState = hubConnection.getConnectionState();

        return connectionState.toString();
    }

    @GetMapping("/test")
    public String test(){
        hubConnection.send(CommContruct.test_url);

        return "test";
    }

    @GetMapping("/event")
    public String event(){
        hubConnection.send(CommContruct.event_url);

        return "event";
    }
}
